package coalescent.recursion.listener;

import coalescent.recursion.RecursionComputer;
import coalescent.recursion.RecursionEvent;
import coalescent.statistic.AC;

import java.util.*;

/**
 *
 *
 * @version version
 * @author Susanta Tewari
 * @history Created on 11/7/12
 * @since 1.4.0
 */
public class FwdCountChecker extends RecursionComputer<Collection<AC<?, ?>>, Void> {

    private final Map<AC<?, ?>, Collection<AC<?, ?>>> callers = new HashMap<>();

    @Override
    public void onInitRecursion(final RecursionEvent event) {

        final AC<?, ?> ac = event.getSrc().getStatistic();

        getCache().put(ac, new ArrayList<AC<?, ?>>(2));
    }

    @Override
    public void onPreVisitAnsConfig(final RecursionEvent event) {


        // get sample & ancestral config
        final AC<?, ?> sampleConfig    = event.getSampleConfig();
        final AC<?, ?> ancestralConfig = event.getAncestralConfig();


        // add callers.
        if (!getCache().containsKey(ancestralConfig))
            getCache().put(ancestralConfig, new ArrayList<AC<?, ?>>());

        getCache().get(ancestralConfig).add(sampleConfig);
    }

    /**
     *
     * @param callee statistic that is called
     * @return total number of callers for the specified callee
     */
    public int getCallerCount(final AC<?, ?> callee) {
        return getCache().get(callee).size();
    }

    public Set<AC> getCallee() {
        return getCache().keySet();
    }
}
